﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ReLogic.OS;
using Terraria.GameContent.UI.Elements;
using Terraria.Graphics;
using Terraria.Localization;
using Terraria.UI;

namespace Terraria.ModLoader.UI
{
	// Token: 0x020002A1 RID: 673
	internal class UIModItem : UIPanel
	{
		// Token: 0x06001AE3 RID: 6883 RVA: 0x0042CD60 File Offset: 0x0042AF60
		public UIModItem(LocalMod mod)
		{
			this.mod = mod;
			this.BorderColor = new Color(89, 116, 213) * 0.7f;
			this.dividerTexture = TextureManager.Load("Images/UI/Divider");
			this.innerPanelTexture = TextureManager.Load("Images/UI/InnerPanelBackground");
			this.Height.Set(90f, 0f);
			this.Width.Set(0f, 1f);
			base.SetPadding(6f);
			string text = mod.DisplayName + " v" + mod.modFile.version;
			if (mod.modFile.tModLoaderVersion < new Version(0, 10))
			{
				text += string.Format(" [c/FF0000:({0})]", Language.GetTextValue("tModLoader.ModOldWarning"));
			}
			if (mod.modFile.HasFile("icon.png"))
			{
				Texture2D texture2D = Texture2D.FromStream(Main.instance.GraphicsDevice, new MemoryStream(mod.modFile.GetFile("icon.png")));
				if (texture2D.Width == 80 && texture2D.Height == 80)
				{
					this.modIcon = new UIImage(texture2D);
					this.modIcon.Left.Set(0f, 0f);
					this.modIcon.Top.Set(0f, 0f);
					base.Append(this.modIcon);
					this.modIconAdjust += 85;
				}
			}
			this.modName = new UIText(text, 1f, false);
			this.modName.Left.Set((float)this.modIconAdjust + 10f, 0f);
			this.modName.Top.Set(5f, 0f);
			base.Append(this.modName);
			UITextPanel<string> uitextPanel = new UITextPanel<string>(Language.GetTextValue("tModLoader.ModsMoreInfo"), 1f, false);
			uitextPanel.Width.Set(100f, 0f);
			uitextPanel.Height.Set(30f, 0f);
			uitextPanel.Left.Set(430f, 0f);
			uitextPanel.Top.Set(40f, 0f);
			uitextPanel.PaddingTop -= 2f;
			uitextPanel.PaddingBottom -= 2f;
			uitextPanel.OnMouseOver += UICommon.FadedMouseOver;
			uitextPanel.OnMouseOut += UICommon.FadedMouseOut;
			uitextPanel.OnClick += this.Moreinfo;
			base.Append(uitextPanel);
			this.button2 = new UITextPanel<string>(mod.Enabled ? Language.GetTextValue("tModLoader.ModsDisable") : Language.GetTextValue("tModLoader.ModsEnable"), 1f, false);
			this.button2.Width.Set(100f, 0f);
			this.button2.Height.Set(30f, 0f);
			this.button2.Left.Set(uitextPanel.Left.Pixels - this.button2.Width.Pixels - 5f, 0f);
			this.button2.Top.Set(40f, 0f);
			this.button2.PaddingTop -= 2f;
			this.button2.PaddingBottom -= 2f;
			this.button2.OnMouseOver += UICommon.FadedMouseOver;
			this.button2.OnMouseOut += UICommon.FadedMouseOut;
			this.button2.OnClick += this.ToggleEnabled;
			base.Append(this.button2);
			string[] modRefs = (from x in mod.properties.modReferences
			select x.mod).ToArray<string>();
			if (modRefs.Length != 0 && !mod.Enabled)
			{
				string arg = string.Join<BuildProperties.ModReference>(", ", mod.properties.modReferences);
				Texture2D texture = Texture2D.FromStream(Main.instance.GraphicsDevice, Assembly.GetExecutingAssembly().GetManifestResourceStream("Terraria.ModLoader.UI.ButtonExclamation.png"));
				UIHoverImage uihoverImage = new UIHoverImage(texture, Language.GetTextValue("tModLoader.ModDependencyClickTooltip", arg));
				uihoverImage.Left.Set(this.button2.Left.Pixels - 24f, 0f);
				uihoverImage.Top.Set(47f, 0f);
				uihoverImage.OnClick += delegate(UIMouseEvent a, UIElement b)
				{
					LocalMod[] source = ModLoader.FindMods();
					List<string> list = new List<string>();
					string[] modRefs = modRefs;
					for (int j = 0; j < modRefs.Length; j++)
					{
						string modRef = modRefs[j];
						ModLoader.EnableMod(modRef);
						if (!source.Any((LocalMod m) => m.Name == modRef))
						{
							list.Add(modRef);
						}
					}
					Main.menuMode = 10000;
					if (list.Any<string>())
					{
						Interface.infoMessage.SetMessage(Language.GetTextValue("tModLoader.ModDependencyModsNotFound", string.Join(",", list)));
						Interface.infoMessage.SetGotoMenu(10000, null);
						Main.menuMode = 10013;
					}
				};
				base.Append(uihoverImage);
			}
			if (mod.modFile.ValidModBrowserSignature)
			{
				this.keyImage = new UIHoverImage(Main.itemTexture[327], Language.GetTextValue("tModLoader.ModsOriginatedFromModBrowser"));
				this.keyImage.Left.Set(-20f, 1f);
				base.Append(this.keyImage);
			}
			if (mod.properties.beta)
			{
				this.keyImage = new UIHoverImage(Main.itemTexture[329], Language.GetTextValue("tModLoader.BetaModCantPublish"));
				this.keyImage.Left.Set(-10f, 1f);
				base.Append(this.keyImage);
			}
			Mod mod2 = ModLoader.GetMod(mod.Name);
			if (mod2 != null)
			{
				this.loaded = true;
				int[] array = new int[]
				{
					mod2.items.Count,
					mod2.npcs.Count,
					mod2.tiles.Count,
					mod2.walls.Count,
					mod2.buffs.Count,
					mod2.mountDatas.Count
				};
				string[] array2 = new string[]
				{
					"ModsXItems",
					"ModsXNPCs",
					"ModsXTiles",
					"ModsXWalls",
					"ModsXBuffs",
					"ModsXMounts"
				};
				int num = -40;
				for (int i = 0; i < array.Length; i++)
				{
					if (array[i] > 0)
					{
						Texture2D texture2 = Main.instance.infoIconTexture[i];
						this.keyImage = new UIHoverImage(texture2, Language.GetTextValue(string.Format("tModLoader.{0}", array2[i]), array[i]));
						this.keyImage.Left.Set((float)num, 1f);
						base.Append(this.keyImage);
						num -= 18;
					}
				}
			}
		}

		// Token: 0x06001AE4 RID: 6884 RVA: 0x0042D418 File Offset: 0x0042B618
		private void GenerateLangTemplate_OnClick(UIMouseEvent evt, UIElement listeningElement)
		{
			Mod mod = ModLoader.GetMod(this.mod.Name);
			Dictionary<string, ModTranslation> dictionary = (Dictionary<string, ModTranslation>)mod.translations;
			IEnumerable<string> enumerable = (from x in mod.items
			where !dictionary.ContainsValue(x.Value.DisplayName)
			select x.Value.DisplayName.Key + "=").Concat(from x in mod.items
			where !dictionary.ContainsValue(x.Value.Tooltip)
			select x.Value.Tooltip.Key + "=").Concat(from x in mod.npcs
			where !dictionary.ContainsValue(x.Value.DisplayName)
			select x.Value.DisplayName.Key + "=").Concat(from x in mod.buffs
			where !dictionary.ContainsValue(x.Value.DisplayName)
			select x.Value.DisplayName.Key + "=").Concat(from x in mod.buffs
			where !dictionary.ContainsValue(x.Value.Description)
			select x.Value.Description.Key + "=").Concat(from x in mod.projectiles
			where !dictionary.ContainsValue(x.Value.DisplayName)
			select x.Value.DisplayName.Key + "=");
			int index = string.Format("Mods.{0}.", this.mod.Name).Length;
			enumerable = from x in enumerable
			select x.Remove(0, index);
			Platform.Current.Clipboard = string.Join("\n", enumerable);
		}

		// Token: 0x06001AE5 RID: 6885 RVA: 0x0042D614 File Offset: 0x0042B814
		private void DrawPanel(SpriteBatch spriteBatch, Vector2 position, float width)
		{
			spriteBatch.Draw(this.innerPanelTexture, position, new Rectangle?(new Rectangle(0, 0, 8, this.innerPanelTexture.Height)), Color.White);
			spriteBatch.Draw(this.innerPanelTexture, new Vector2(position.X + 8f, position.Y), new Rectangle?(new Rectangle(8, 0, 8, this.innerPanelTexture.Height)), Color.White, 0f, Vector2.Zero, new Vector2((width - 16f) / 8f, 1f), SpriteEffects.None, 0f);
			spriteBatch.Draw(this.innerPanelTexture, new Vector2(position.X + width - 8f, position.Y), new Rectangle?(new Rectangle(16, 0, 8, this.innerPanelTexture.Height)), Color.White);
		}

		// Token: 0x06001AE6 RID: 6886 RVA: 0x0042D6F8 File Offset: 0x0042B8F8
		private void DrawEnabledText(SpriteBatch spriteBatch, Vector2 drawPos)
		{
			string text = this.mod.Enabled ? Language.GetTextValue("GameUI.Enabled") : Language.GetTextValue("GameUI.Disabled");
			Color color = this.mod.Enabled ? Color.Green : Color.Red;
			Utils.DrawBorderString(spriteBatch, text, drawPos, color, 1f, 0f, 0f, -1);
		}

		// Token: 0x06001AE7 RID: 6887 RVA: 0x0042D760 File Offset: 0x0042B960
		protected override void DrawSelf(SpriteBatch spriteBatch)
		{
			base.DrawSelf(spriteBatch);
			CalculatedStyle innerDimensions = base.GetInnerDimensions();
			Vector2 vector = new Vector2(innerDimensions.X + 5f + (float)this.modIconAdjust, innerDimensions.Y + 30f);
			spriteBatch.Draw(this.dividerTexture, vector, null, Color.White, 0f, Vector2.Zero, new Vector2((innerDimensions.Width - 10f - (float)this.modIconAdjust) / 8f, 1f), SpriteEffects.None, 0f);
			vector = new Vector2(innerDimensions.X + 10f + (float)this.modIconAdjust, innerDimensions.Y + 45f);
			this.DrawPanel(spriteBatch, vector, 85f);
			this.DrawEnabledText(spriteBatch, vector + new Vector2(10f, 5f));
			if (this.mod.Enabled != this.loaded)
			{
				vector += new Vector2(90f, 5f);
				Utils.DrawBorderString(spriteBatch, Language.GetTextValue("tModLoader.ModReloadRequired"), vector, Color.White, 1f, 0f, 0f, -1);
			}
		}

		// Token: 0x06001AE8 RID: 6888 RVA: 0x0042D894 File Offset: 0x0042BA94
		protected override void DrawChildren(SpriteBatch spriteBatch)
		{
			base.DrawChildren(spriteBatch);
			if (this.modName.IsMouseHovering && this.mod.properties.author.Length > 0)
			{
				string textValue = Language.GetTextValue("tModLoader.ModsByline", this.mod.properties.author);
				float x = Main.fontMouseText.MeasureString(textValue).X;
				Vector2 vector = Main.MouseScreen + new Vector2(16f);
				if (vector.Y > (float)(Main.screenHeight - 30))
				{
					vector.Y = (float)(Main.screenHeight - 30);
				}
				if (vector.X > (float)Main.screenWidth - x)
				{
					vector.X = (float)Main.screenWidth - x - 30f;
				}
				Utils.DrawBorderStringFourWay(spriteBatch, Main.fontMouseText, textValue, vector.X, vector.Y, new Color((int)Main.mouseTextColor, (int)Main.mouseTextColor, (int)Main.mouseTextColor, (int)Main.mouseTextColor), Color.Black, Vector2.Zero, 1f);
			}
		}

		// Token: 0x06001AE9 RID: 6889 RVA: 0x000132A0 File Offset: 0x000114A0
		public override void MouseOver(UIMouseEvent evt)
		{
			base.MouseOver(evt);
			this.BackgroundColor = new Color(73, 94, 171);
			this.BorderColor = new Color(89, 116, 213);
		}

		// Token: 0x06001AEA RID: 6890 RVA: 0x0042C064 File Offset: 0x0042A264
		public override void MouseOut(UIMouseEvent evt)
		{
			base.MouseOut(evt);
			this.BackgroundColor = new Color(63, 82, 151) * 0.7f;
			this.BorderColor = new Color(89, 116, 213) * 0.7f;
		}

		// Token: 0x06001AEB RID: 6891 RVA: 0x0042D99C File Offset: 0x0042BB9C
		internal void ToggleEnabled(UIMouseEvent evt, UIElement listeningElement)
		{
			Main.PlaySound(12, -1, -1, 1, 1f, 0f);
			this.mod.Enabled = !this.mod.Enabled;
			this.button2.SetText(this.mod.Enabled ? Language.GetTextValue("tModLoader.ModsDisable") : Language.GetTextValue("tModLoader.ModsEnable"), 1f, false);
		}

		// Token: 0x06001AEC RID: 6892 RVA: 0x000133F9 File Offset: 0x000115F9
		internal void Enable()
		{
			if (!this.mod.Enabled)
			{
				this.ToggleEnabled(null, null);
			}
		}

		// Token: 0x06001AED RID: 6893 RVA: 0x00013410 File Offset: 0x00011610
		internal void Disable()
		{
			if (this.mod.Enabled)
			{
				this.ToggleEnabled(null, null);
			}
		}

		// Token: 0x06001AEE RID: 6894 RVA: 0x0042DA0C File Offset: 0x0042BC0C
		internal void Moreinfo(UIMouseEvent evt, UIElement listeningElement)
		{
			Main.PlaySound(10, -1, -1, 1, 1f, 0f);
			Interface.modInfo.SetModName(this.mod.DisplayName);
			Interface.modInfo.SetModInfo(this.mod.properties.description);
			Interface.modInfo.SetMod(this.mod);
			Interface.modInfo.SetGotoMenu(10000);
			Interface.modInfo.SetURL(this.mod.properties.homepage);
			Main.menuMode = 10008;
		}

		// Token: 0x06001AEF RID: 6895 RVA: 0x0042DAA0 File Offset: 0x0042BCA0
		public override int CompareTo(object obj)
		{
			UIModItem uimodItem = (UIModItem)obj;
			string displayName = this.mod.DisplayName;
			string displayName2 = uimodItem.mod.DisplayName;
			switch (Interface.modsMenu.sortMode)
			{
			case ModsMenuSortMode.RecentlyUpdated:
				return -1 * this.mod.lastModified.CompareTo(uimodItem.mod.lastModified);
			case ModsMenuSortMode.DisplayNameAtoZ:
				return string.Compare(displayName, displayName2, StringComparison.Ordinal);
			case ModsMenuSortMode.DisplayNameZtoA:
				return -1 * string.Compare(displayName, displayName2, StringComparison.Ordinal);
			default:
				return base.CompareTo(obj);
			}
		}

		// Token: 0x06001AF0 RID: 6896 RVA: 0x0042DB24 File Offset: 0x0042BD24
		public override bool PassFilters()
		{
			if (Interface.modsMenu.filter.Length > 0)
			{
				if (Interface.modsMenu.searchFilterMode == SearchFilter.Author)
				{
					if (this.mod.properties.author.IndexOf(Interface.modsMenu.filter, StringComparison.OrdinalIgnoreCase) == -1)
					{
						return false;
					}
				}
				else if (this.mod.DisplayName.IndexOf(Interface.modsMenu.filter, StringComparison.OrdinalIgnoreCase) == -1)
				{
					return false;
				}
			}
			if (Interface.modsMenu.modSideFilterMode != ModSideFilter.All && this.mod.properties.side != (ModSide)(Interface.modsMenu.modSideFilterMode - 1))
			{
				return false;
			}
			switch (Interface.modsMenu.enabledFilterMode)
			{
			default:
				return true;
			case EnabledFilter.EnabledOnly:
				return this.mod.Enabled;
			case EnabledFilter.DisabledOnly:
				return !this.mod.Enabled;
			}
		}

		// Token: 0x0400181F RID: 6175
		private readonly LocalMod mod;

		// Token: 0x04001820 RID: 6176
		private readonly Texture2D dividerTexture;

		// Token: 0x04001821 RID: 6177
		private int modIconAdjust;

		// Token: 0x04001822 RID: 6178
		private readonly Texture2D innerPanelTexture;

		// Token: 0x04001823 RID: 6179
		private readonly UIText modName;

		// Token: 0x04001824 RID: 6180
		private readonly UITextPanel<string> button2;

		// Token: 0x04001825 RID: 6181
		private UIImage modIcon;

		// Token: 0x04001826 RID: 6182
		private readonly UIHoverImage keyImage;

		// Token: 0x04001827 RID: 6183
		private bool loaded;
	}
}
